473,469 Members | 1,495 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Bug in struct.pack?

from struct import pack
pack("B", 1) '\x01' pack("BB", 0, 1) '\x00\x01' pack("BI", 0, 1) '\x00\x00\x00\x00\x01\x00\x00\x00' calcsize("BI") 8 calcsize("BB") 2

Why does an unsigned char suddenly become 4 bytes long when you
include an unsigned int in the format string? It's consistent
behaviour but it's incorrect.

Also.
calcsize('BL') 8 calcsize('BBL') 8 calcsize('BBBL') 8 calcsize('BBBBL') 8 calcsize('BBBBBL') 12 pack("BBBL", 255,255,255,0) '\xff\xff\xff\x00\x00\x00\x00\x00' ####### That's 3 255's and 5(!?!?)
0's pack("BBBBL", 255,255,255,255,0)

'\xff\xff\xff\xff\x00\x00\x00\x00' ######### 4 255's and 4 0's!

Which is all kinds of wrong.

BL should be 9
BBL should be 10
.....

Python 2.4.1 (#2, May 5 2005, 11:32:06)
[GCC 3.3.5 (Debian 1:3.3.5-12)] on linux2

Same behaviour on my PowerBook using

Python 2.3.5 (#1, Mar 20 2005, 20:38:20)
[GCC 3.3 20030304 (Apple Computer, Inc. build 1809)] on darwin

sizeof(unsigned long) should be 8 on both of these platforms
sizeof(unsigned char) should be 1 on both as well

So am I just being stupid and not specifying something I should be?
Or is struct really that broken?
Jan 11 '06 #1
1 2329
[Alex Stapleton]
from struct import pack
>>> pack("B", 1) '\x01' >>> pack("BB", 0, 1) '\x00\x01' >>> pack("BI", 0, 1) '\x00\x00\x00\x00\x01\x00\x00\x00' >>> calcsize("BI") 8 >>> calcsize("BB") 2

Why does an unsigned char suddenly become 4 bytes long when you
include an unsigned int in the format string? It's consistent
behaviour but it's incorrect.


You're seeing native alignment come into play.
Specify a standard alignment to avoid padding.
pack("BI", 0, 1) '\x00\x00\x00\x00\x01\x00\x00\x00' pack("=BI", 0, 1)

'\x00\x01\x00\x00\x00'

All is explained in the docs:
http://docs.python.org/lib/module-struct.html
Raymond

Jan 11 '06 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Josiah Carlson | last post by:
Good day everyone, I have produced a patch against the latest CVS to add support for two new formatting characters in the struct module. It is currently an RFE, which I include a link to at the...
10
by: Giovanni Bajo | last post by:
Hello, given the ongoing work on struct (which I thought was a dead module), I was wondering if it would be possible to add an API to register custom parsing codes for struct. Whenever I use it...
2
by: Jansson Christer | last post by:
Hi all, I have discovered that in my Python 2.4.1 installation (on Solaris 8), struct.pack handles things in a way that seems inconsistent to me. I haven't found any comprehensible...
6
by: Jack | last post by:
Hi, All, is it possible to send a struct using the the send function. Here is what I mean typedef struct{ int ID; char name; }sampleStruct; int main(){
4
by: Joshua J. Kugler | last post by:
I'm trying to put some values into a struct. Some of these values are NaN and Inf due to the nature of the data. As you well may know, struct (and other things) in Python <= 2.4 doesn't support...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.